<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="utf-8">
<title>SmartPing Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<link href="assets/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="assets/css/font-awesome.css" rel="stylesheet">
<link href="assets/css/style.css" rel="stylesheet">
<link href="assets/css/pages/dashboard.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
<div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
      <span id="cloudbrand" class="brand" style="margin-right: -15px;"></span><a class="brand" href="index.html">SmartPing Dashbord<span id="agentname"></span></a>
      <span id="banner_last_ck_time" class="pull-right " style="margin-top: 10px;"></span>
    </div>
    <!-- /container -->
  </div>
  <!-- /navbar-inner --> 
</div>
<!-- /navbar -->
<div class="subnavbar">
  <div class="subnavbar-inner">
    <div class="container">
      <ul class="mainnav">
        <li   class="active"><a href="index.html"><i class="icon-mail-forward"></i><span>正向Ping</span> </a> </li>
        <li><a href="reverse.html"><i class="icon-mail-reply"></i><span>反向Ping</span> </a> </li>
        <li><a href="topology.html"><i class="icon-bar-chart"></i><span>Ping拓扑</span> </a> </li>
        <li><a href="mapping.html"><i class="icon-map-marker"></i><span>全国延迟</span> </a> </li>
        <li ><a href="tools.html"><i class="icon-wrench"></i><span>检测工具</span> </a> </li>
        <li><a id="cfgUrl" href="config.html"><i class="icon-cog"></i><span>系统配置</span> </a> </li>
      </ul>
    </div>
    <!-- /container --> 
  </div>
  <!-- /subnavbar-inner --> 
</div>
<!-- /subnavbar -->
<div class="main" style="margin-top:-20px;">
  <div class="main-inner">
    <div class="container">

      <div class="row">
        <div  id="main" class="span10" >
          <div id="lineChart"></div>
          <!-- ping charts -->
        </div>
        <!-- /span10 -->
        <div class="span2">
          <div class="widget widget-table action-table">
            <div class="widget-header"> <i class="icon-th-list"></i>
              <h3 style="margin-top: -5px;">节点列表 </h3>
            </div>
            <!-- /widget-header -->
            <div class="widget-content">
              <table class="table table-striped table-bordered">
                <thead></thead>
                <tbody class="agentlist"></tbody>
              </table>
            </div>
            <!-- /widget-content -->
          </div>
        </div>
      </div>

    </div>
    <!-- /container --> 
  </div>
  <!-- /main-inner -->
</div>
<!-- /main -->

<div class="footer">
  <div class="footer-inner">
    <div class="container">
      <div class="row">
        <div class="span12"> &copy; 2017 - 2020 <a target="_blank" href="http://smartping.org" >SmartPing.org</a> <span style="float:right" id="verion"></span></div>
        <!-- /span12 --> 
      </div>
      <!-- /row --> 
    </div>
    <!-- /container --> 
  </div>
  <!-- /footer-inner --> 
</div>

<div id="charts" class="modal fade" tabindex="-1" style="width: 830px;height:500px;">
  <input type="hidden" id="pannelurl" value=""/>
  <div class="modal-dialog">
    <div class="modal-content" >
        <div style="text-align:center;padding-top:10px;">
          <b>StartTime:</b>&nbsp;<input id="starttime" type='text'  onClick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})" style="width:120px;" >
          <b>EndTime:</b>&nbsp;<input id="endtime" type='text'  onClick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})"  style="width:120px;" >
          &nbsp;<input class="sgraph" type="button" value="Submit" />
        </div>
        <div class="modal-body" id="pannel-show" style="width: 800px;height:500px;">
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- /footer --> 
<!-- Le javascript
================================================== --> 
<!-- Placed at the end of the document so the pages load faster -->
<script src="assets/js/jquery-1.7.2.min.js"></script>
<script src="assets/js/bootstrap.js"></script>
<script src="assets/js/echarts.min.js"></script>
<script language="JavaScript" type="text/javascript" src="assets/js/My97DatePicker/WdatePicker.js"></script>
<script src="assets/js/funcs.js"></script>
<script>
    getLineChart("");
    function changeAgent(n,url){
        $(".alerticon-" + n).addClass("icon-spinner").addClass("icon-spin").addClass("animated");
        getLineChart(url)
    }
    function getLineChart(url){
      if(url!=""){
          url="/api/proxy.json?g="+url
      }
      $.getJSON(url+"/api/config.json",function(result){
          $("#lineChart").html("");
          $(".agentlist").html("");
          $("#agentname").html(" ("+result["Name"]+")")
          AgentMode(result["Mode"])
          $("#verion").html("Version: "+result["Ver"])
          $.each(result["Network"], function(i, network){
              if(network["Smartping"]){
                  $(".agentlist").append("<tr><td><i class='alerticon-" + network["Name"] + "'></i>&nbsp;" +
                      "<a onclick=changeAgent('" + network["Name"] + "','http://" + network["Addr"] + ":" + result["Port"] + "')>" + network["Name"] + "</a></td></tr>");
              }
          });
          $.each(result["Network"][result["Addr"]]["Ping"], function(i, mAddr){
              $("#lineChart").append("<div class='img showcharts' " +
                  "style='text-align:center;float:left;width:30.3%;height:130px;margin-right:3%;margin-top:20px;;background-size:100% 100%;' " +
                  "imgsrc='/api/graph.png?g=http://"+result["Addr"]+":"+result["Port"]+"/api/ping.json?ip="+mAddr+"' " +
                  "apiurl='/api/proxy.json?g=http://"+result["Addr"]+":"+result["Port"]+"/api/ping.json?ip="+mAddr+"' >" +
                  "<b style='font-color:#333333'>"+result["Name"]+"->"+result["Network"][mAddr]["Name"]+"</b></div>");
          });
          layzeImg();
      }).fail(function (xhr, status) {
          alert(xhr.responseText);
          Refresh();
      });
    }
    $("#main").on("click",".showcharts",function(){
        //window.clearInterval(int)
        var modalWidth = $("#charts").width();
        var left = "-" + parseInt(modalWidth) / 2 + "px";
        $('#charts').modal('show').css({"margin-left":left});
        url=$(this).attr("apiurl");
        $("#pannelurl").attr("value",url);
        $.get(url).done(function (data) {
            //var data = JSON.parse(data);
            myChart.setOption({
                xAxis: {
                    data: data.lastcheck
                },
                series: [{
                    name: 'maxDelay',
                    data: data.maxdelay
                },
                    {
                        name: 'minDelay',
                        data: data.mindelay
                    },
                    {
                        name: 'avgDelay',
                        data: data.avgdelay
                    },
                    {
                        name: 'lossPk',
                        data: data.losspk
                    }]
            });
        });
    });
    $(".sgraph").click(function(){
        start   = $("#starttime").val();
        endtime = $("#endtime").val();
        url = $("#pannelurl").attr("value");
        $.get(url+"%26starttime="+start+"%26endtime="+endtime).done(function (data) {
            //console.log(data)
            myChart.setOption({
                xAxis: {
                    data: data.lastcheck
                },
                series: [{
                    name: 'maxDelay',
                    data: data.maxdelay
                },
                    {
                        name: 'minDelay',
                        data: data.mindelay
                    },
                    {
                        name: 'avgDelay',
                        data: data.avgdelay
                    },
                    {
                        name: 'lossPk',
                        data: data.losspk
                    }]
            });
        });
    });
    opt={
        title: {
            text: ''
        },
        legend: {
            data:['maxDelay','avgDelay','minDelay','lossPk'],
            selected: {
                'maxDelay' : false,
                'minDelay' : false
            }
        },
        toolbox: {
            feature: {
                dataView: {},
                saveAsImage: {
                    pixelRatio: 2
                }
            }
        },
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            data: []
        },
        dataZoom: [{}],
        yAxis: [{
            type: 'value',
            name: 'Delay',
            position: 'left'
        }, {
            type: 'value',
            name: 'Package(LOSS)',
            min: 0,
            max: 100,
            position: 'right',
            axisLabel: {
                formatter: '{value} %'
            }
        }],
        series: [{
            name: 'maxDelay',
            type: 'line',
            animation: false,
            areaStyle: {
                normal: {}
            },
            lineStyle: {
                normal: {
                    width: 0
                }
            },
            data: []
        },
            {
                name: 'minDelay',
                type: 'line',
                animation: false,
                areaStyle: {
                    normal: {}
                },
                lineStyle: {
                    normal: {
                        width: 0
                    }
                },
                data: []
            },
            {
                name: 'avgDelay',
                type: 'line',
                data: [],
                itemStyle: {
                    normal: {
                        color : '#00CC66'
                    }
                },
                animation: false,
                areaStyle: {
                    normal: {}
                },
                lineStyle: {
                    normal: {
                        width: 0
                    }
                }
            },
            {
                name: 'lossPk',
                type: 'line',
                yAxisIndex: 1,
                data: [],
                itemStyle: {
                    normal: {
                        color : '#FF0000'
                    }
                },
                animation: false,
                areaStyle: {
                    normal: {}
                },
                lineStyle: {
                    normal: {
                        width: 0
                    }
                }
            }]
    }
    var myChart = echarts.init(document.getElementById('pannel-show'));
    myChart.setOption(opt);
    function layzeImg() {
        var aImg = document.querySelectorAll('div.img');
        var len = aImg.length;
        var n = 0;
        for (var i = n; i < len; i++) {
            if (aImg[i].offsetTop < document.documentElement.clientHeight + (document.body.scrollTop || document.documentElement.scrollTop)) {
                if (aImg[i].style.backgroundImage == '') {
                    aImg[i].style.backgroundImage = "url('"+aImg[i].getAttribute('imgsrc')+"')";
                }
                n = i + 1;
            }
        }
        window.onscroll = function () {
            for (var i = n; i < len; i++) {
                if (aImg[i].offsetTop < document.documentElement.clientHeight + (document.body.scrollTop || document.documentElement.scrollTop)) {
                    if (aImg[i].style.backgroundImage == '') {
                        aImg[i].style.backgroundImage = "url('"+aImg[i].getAttribute('imgsrc')+"')";
                    }
                    n = i + 1;
                }
            }
        };
    };
</script>
</body>
</html>
